package com.xinghai.web.controller.api;

import cn.dev33.satoken.stp.StpUtil;
import com.github.pagehelper.PageHelper;
import com.xinghai.common.core.controller.BaseController;
import com.xinghai.common.core.domain.AjaxResult;
import com.xinghai.common.utils.DataUtil;
import com.xinghai.common.utils.DateUtils;
import com.xinghai.common.utils.PageUtils;
import com.xinghai.common.utils.StringUtils;
import com.xinghai.system.domain.*;
import com.xinghai.system.domain.entity.WithdrawalDTO;
import com.xinghai.system.domain.entity.WithdrawalUpdateDTO;
import com.xinghai.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.YearMonth;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 钱包api接口
 *
 * @author xinghai
 * @date 2024-12-04
 */
@RestController
@RequestMapping("/api/wallet")
public class QdWalletApiController extends BaseController
{

    @Autowired
    private IQdUserWalletService userWalletService;

    @Autowired
    private IQdUserWalletLogService userWalletLogService;

    @Autowired
    private IQdUserService userService;

    @Autowired
    private IQdStoreService storeService;

    @Autowired
    private IQdUserWithdrawalService withdrawalService;


    /**
     * 获取钱包信息
     */
    @GetMapping("/getWalletInfo")
    public AjaxResult getWalletInfo()
    {
        String loginId = StpUtil.getLoginId("");
        QdUserWallet userWallet = userWalletService.getByUserId(loginId);
        Map<String,Object> map = new HashMap<>();
        QdUserWalletLog qdUserWalletLog = new QdUserWalletLog();
        qdUserWalletLog.setUserId(Long.valueOf(loginId));
        qdUserWalletLog.setType("1");
        qdUserWalletLog.setParams(DataUtil.getCreateTimeParams());
        map.put("todayMoney",userWalletLogService.sumMoney(qdUserWalletLog));
        map.put("amount",userWallet.getWithdrawalWallet());//可提现金额
        map.put("allAmount",userWallet.getAllWallet());//累计佣金
        return success(map);
    }

    /**
     * 获取收入明细
     */
    @GetMapping("/getWalletLog")
    public AjaxResult getWalletLog(Integer pageNum, Integer pageSize,String type)
    {
        String loginId = StpUtil.getLoginId("");
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy("create_time desc");
        QdUserWalletLog qdUserWalletLog = new QdUserWalletLog();
        if ("2".equals(type)){
            qdUserWalletLog.setType("2");
        }else {
            qdUserWalletLog.setType("1,3");
        }
        qdUserWalletLog.setUserId(Long.valueOf(loginId));
        List<QdUserWalletLog> list = userWalletLogService.list(qdUserWalletLog);
        return success(getDataTable(list));
    }

    /**
     * 获取提现备注
     */
    @GetMapping("/getWithdrawalRemark")
    public AjaxResult getWithdrawal()
    {
        String loginId = StpUtil.getLoginId("");
        QdUser user = userService.getByLoginId(loginId);
        if (user.getAscriptionStore()==null){
            return success("无");
        }
        QdStore store = storeService.getById(user.getAscriptionStore());
        Map<String,Object> map = new HashMap<>();
        map.put("remark",store.getWithdrawalRemark());
        map.put("status",store.getWithdrawalStatus());
        map.put("minAmount",store.getMinWithdrawalAmount());
        map.put("maxAmount",store.getMaxWithdrawalAmount());
        return success(map);
    }

    /**
     * 获取提现备注
     */
    @PostMapping("/withdrawal")
    public AjaxResult withdrawal(@Validated @RequestBody WithdrawalDTO withdrawalDTO)
    {
        String loginId = StpUtil.getLoginId("");
        withdrawalDTO.setUserId(loginId);
        withdrawalService.withdrawal(withdrawalDTO);
        return success("操作成功");
    }

    /**
     * 查询提现申请列表
     */
    @GetMapping("/getWithdrawalList")
    public AjaxResult getWithdrawalList(String search ,String date)
    {
        String loginId = StpUtil.getLoginId("");
        QdUser user = userService.getByLoginId(loginId);
        QdUserWithdrawal qdUserWithdrawal = new QdUserWithdrawal();
        qdUserWithdrawal.setStoreId(user.getAscriptionStore());
//        qdUserWithdrawal.setStatus("0");
        qdUserWithdrawal.setSearch(search);
        qdUserWithdrawal.setParams(DataUtil.getCreateTimeParams2(date));
        //方式1
        //qdUserWithdrawal.setParams();
        List<QdUserWithdrawal> list = withdrawalService.list(qdUserWithdrawal);
        return success(list);
    }

    /**
     * 审核
     */
    @PostMapping("/updateStatus")
    public AjaxResult updateStatus(@Validated @RequestBody WithdrawalUpdateDTO dto)
    {
        if (!"1".equals(dto.getStatus())&&!"2".equals(dto.getStatus())){
            return error("参数不全");
        }
        if(StringUtils.isBlank(dto.getIds())){
            String loginId = StpUtil.getLoginId("");
            QdUser user = userService.getByLoginId(loginId);
            QdUserWithdrawal qdUserWithdrawal = new QdUserWithdrawal();
            qdUserWithdrawal.setStoreId(user.getAscriptionStore());
            qdUserWithdrawal.setStatus("0");
            qdUserWithdrawal.setCreateTime(DateUtils.parseDate(dto.getDate()));
            List<QdUserWithdrawal> list = withdrawalService.list(qdUserWithdrawal);
            String string = list.stream().map(m -> String.valueOf(m.getId())).collect(Collectors.joining(","));
            dto.setIds(string);
        }
        if(StringUtils.isBlank(dto.getIds())){
            return error("没有可处理的数据");
        }
        return success(withdrawalService.updateStatus(dto));
    }
}
